/**
 * @author Tobias Boehm
 *
 * 12.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.client.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.map.AbsMapOverview;
import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.client.AbsClientMessage;

/**
 * This message should be used to open a new game on the game server. It contains
 * a {@link #mMapID} and the {@link #mMapVersion} of this map which is available
 * on the client side which should be used to open the new game.
 * 
 * @author Tobias Boehm
 *
 */
public class ClientPrepareMatchRequestMessage extends AbsClientMessage implements ICopyable<ClientPrepareMatchRequestMessage> {

	private String mMapID = "";
	private int mMapVersion = -1;
	
		
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_CLIENT_PREPARE_MATCH_REQUEST;
	}

	@Override
	protected void onReadTransmissionClientData(final DataInputStream pDataInputStream)
			throws IOException {

		this.mMapID = pDataInputStream.readUTF();
		
		this.mMapVersion = pDataInputStream.readInt();
	}

	@Override
	protected void onWriteTransmissionClientData(final 
			DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeUTF(this.mMapID);
		
		pDataOutputStream.writeInt(this.mMapVersion);
	}

	public void setMap(final AbsMapOverview pMapOverview){
		
		this.mMapID = pMapOverview.getMapID();
		this.mMapVersion = pMapOverview.getMapVersion();
	}
	
	/**
	 * @return the mMapID
	 */
	public String getMapID() {
		return mMapID;
	}

	/**
	 * @return the mMapVersion
	 */
	public int getMapVersion() {
		return mMapVersion;
	}

	@Override
	public ClientPrepareMatchRequestMessage copy() {
		
		final ClientPrepareMatchRequestMessage copy = new ClientPrepareMatchRequestMessage();
		
		copy.mMapID = this.mMapID;
		copy.mMapVersion = this.mMapVersion;
		copy.mPlayerID = this.mPlayerID;
		
		return copy;
	}
}
